package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.admin.pojo.dto.RaidersCorpCountDTO;
import com.xbongbong.admin.pojo.dto.RaidersCorpCountListDTO;
import com.xbongbong.admin.pojo.dto.RaidersCorpScoreDTO;
import com.xbongbong.admin.pojo.dto.RaidersScoreDTO;
import com.xbongbong.admin.pojo.dto.RaidersStatusGetDTO;
import com.xbongbong.admin.pojo.vo.RaidersCorpCountListVO;
import com.xbongbong.admin.pojo.vo.RaidersCorpCountVO;
import com.xbongbong.admin.pojo.vo.RaidersCorpScoreVO;
import com.xbongbong.admin.pojo.vo.RaidersScoreVO;
import com.xbongbong.admin.pojo.vo.RaidersStatusGetVO;
import com.xbongbong.admin.service.RaidersCorpService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 公司关联攻略
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/9/26 10:32
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/raiders/corp")
public class RaidersCorpController {
    @Resource
    private RaidersCorpService raidersCorpService;

    @RequestMapping(value = "/score", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid RaidersCorpScoreDTO raidersCorpScoreDTO, BindingResult br) {
        XbbResponse<RaidersCorpScoreVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersCorpScoreVO raidersCorpScoreVO = raidersCorpService.score(raidersCorpScoreDTO);
                response = new XbbResponse<>(raidersCorpScoreVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/count/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String countList(@RequestBody @Valid RaidersCorpCountListDTO raidersCorpCountListDTO, BindingResult br) {
        XbbResponse<RaidersCorpCountListVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersCorpCountListVO raidersCorpCountListVO = raidersCorpService.countList(raidersCorpCountListDTO);
                response = new XbbResponse<>(raidersCorpCountListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/count", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String count(@RequestBody @Valid RaidersCorpCountDTO raidersCorpCountDTO, BindingResult br) {
        XbbResponse<RaidersCorpCountVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersCorpCountVO raidersCorpCountVO = raidersCorpService.count(raidersCorpCountDTO);
                response = new XbbResponse<>(raidersCorpCountVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/getScore", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getScore(@RequestBody @Valid RaidersScoreDTO raidersScoreDTO, BindingResult br) {
        XbbResponse<RaidersScoreVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersScoreVO raidersScoreVO = raidersCorpService.getScore(raidersScoreDTO);
                response = new XbbResponse<>(raidersScoreVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/status/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String indexCardGet(@RequestBody @Valid RaidersStatusGetDTO raidersStatusGetDTO, BindingResult br) {
        XbbResponse<RaidersStatusGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersStatusGetVO raidersStatusGetVO = raidersCorpService.indexCardGet(raidersStatusGetDTO);
                response = new XbbResponse<>(raidersStatusGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
